package com.example.tabletexample;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ItemListFragment extends ListFragment{
	
	public interface ItemListFragmentListener{
		public void onItemSelected(String item);
	}
	
	String[] items = new String[] {
	        "Item 1",
	        "Item 2",
	        "Item 3",
	        "Item 4",
	        "Item 5",
	        "Item 6",
	        "Item 7",
	        "Item 8",
	        "Item 9",
	        "Item 10"
	    };
	
	private ItemListFragmentListener listener;
	
	public ItemListFragment(){
		listener = null;
	}
	
	public ItemListFragment(ItemListFragmentListener lis){
		listener = lis;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater,
    						ViewGroup container,
    						Bundle savedInstanceState) {
    	
    	//Create an ArrayAdapter
    	ArrayAdapter<String> adapter
    						= new ArrayAdapter<String>(
    								inflater.getContext(),
    								android.R.layout.simple_list_item_1,
    								items);
 
    	//Set the list adapter to display the data
        setListAdapter(adapter);
        
        return super.onCreateView(inflater, container, savedInstanceState);
    }

	@Override
	public void onListItemClick(ListView l, View view, int position, long id) {
		TextView textView = (TextView) view;
		String text = (String) textView.getText();
		
		if(listener != null){
			listener.onItemSelected(text);
		}
	}

}
